package com.xiaoxi.checkpasswordserver.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @ClassName SpecialCharCheck
 * @Description
 * @Author xiaoxi
 * @Date 2022/7/11 21:36
 * @Version 1.0
 */
@Component
public class SpecialCharCheck implements IPasswordCheck {
    public static String SPECIAL_CHAR = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
    @Value("${bms.password-check.check-special-character}")
    private boolean checkSpecialCharacter;

    public SpecialCharCheck() {
    }

    public CheckResult check(String password, CheckResult checkResult) {

        if (this.checkSpecialCharacter) {

            char[] chPass = password.toCharArray();
            boolean flag = false;
            int specialCount = 0;

            for (char pass : chPass) {
                if (SPECIAL_CHAR.indexOf(pass) != -1) {
                    ++specialCount;
                }
            }

            if (specialCount >= 1) {
                flag = true;
            }

            if (!flag) {
                checkResult.getConfirmInfo().add("#$%&'()*+等特殊字符");
            } else {
                checkResult.setConfirmCount(checkResult.getConfirmCount() + 1);
            }

        }
        return checkResult;
    }
}
